<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="22">
          <el-form-item>
            <el-input v-model="searchForm.username" style="width:180px" placeholder="登录账号" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchForm.empName" style="width:180px" placeholder="员工姓名" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchForm.systemModules" style="width:180px" placeholder="系统模块" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.operationType" clearable style="width:190px" filterable placeholder="操作类型">
              <el-option value="新增" label="新增">新增</el-option>
              <el-option value="修改" label="修改">修改</el-option>
              <el-option value="删除" label="删除">删除</el-option>
              <el-option value="强退" label="强退">强退</el-option>
              <el-option value="重置密码" label="重置密码">重置密码</el-option>
              <el-option value="注销" label="注销">注销</el-option>
              <el-option value="启用" label="启用">启用</el-option>
              <el-option value="批量导入" label="批量导入">批量导入</el-option>
              <el-option value="导出" label="导出">导出</el-option>
              <el-option value="全部恢复默认菜单权限" label="全部恢复默认菜单权限">全部恢复默认菜单权限</el-option>
              <el-option value="恢复默认菜单权限" label="恢复默认菜单权限">恢复默认菜单权限</el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.operationStatus" clearable style="width:180px" filterable placeholder="操作状态">
              <el-option value="0" label="成功">成功</el-option>
              <el-option value="1" label="失败">失败</el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="iconfont icon-chaxun1" @click="handleSearch"> 查询</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="2" class="search-right">
          <el-button type="text" icon="el-icon-refresh" @click="handleSearch">刷新</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      border
      :data="sysOperationInfoData"
      stripe
      highlight-current-row
      :header-cell-style="{background:'#ECF3FC'}"
    >
      <el-table-column prop="id" label="日志编号" align="center" />
      <el-table-column prop="systemModules" label="系统模块" align="center" />
      <el-table-column prop="operationType" label="操作类型" width="170px" align="center">
        <template slot-scope="{row}">
          <el-tag type="info">{{row.operationType}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="requestMethod" label="请求方式" align="center" />
      <el-table-column prop="username" label="登录账号" align="center" />
      <el-table-column prop="empName" label="员工姓名" align="center" />
      <el-table-column prop="deptName" label="部门名称" align="center" />
      <el-table-column prop="ipAddress" label="操作地址" width="120px" align="center" />
      <el-table-column prop="operationLocation" label="操作地点" width="120px" align="center" />
      <el-table-column prop="operationStatus" label="操作状态" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.operationStatus === '0'" type="success">成功</el-tag>
          <el-tag v-if="row.operationStatus === '1'" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="操作时间" width="160px" align="center" />
      <el-table-column label="操作" fixed="right" align="center">
        <template slot-scope="{row}">
          <el-button type="text" @click="seeOperationInfo(row)">详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"/>
    </div>

    <!-- 详细 -->
    <el-dialog title="操作日志详细" :visible.sync="seeOperationInfoVisible" width="35%" :close-on-click-modal="false">
      <div>
        <el-form :model="seeOperationInfoForm" label-width="90px">
          <el-row>
            <el-col :span="11">
              <el-form-item prop="systemModules" label="系统模块:">
                <span>{{seeOperationInfoForm.systemModules}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="requestAddress" label="请求地址:">
                <span>{{seeOperationInfoForm.requestAddress}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="operationType" label="操作类型:">
                <el-tag type="info">{{seeOperationInfoForm.operationType}}</el-tag>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="requestMethod" label="请求方式:">
                <span>{{seeOperationInfoForm.requestMethod}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="username" label="登录账号:">
                <span>{{seeOperationInfoForm.username}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="empName" label="员工姓名:">
                <span>{{seeOperationInfoForm.empName}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="deptName" label="部门名称:">
                <span>{{seeOperationInfoForm.deptName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="ipAddress" label="操作地址:">
                <span>{{seeOperationInfoForm.ipAddress}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="operationLocation" label="操作地点:">
                <span>{{seeOperationInfoForm.operationLocation}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="operationStatus" label="操作状态:">
                <el-tag v-if="seeOperationInfoForm.operationStatus === '0'" type="success">成功</el-tag>
                <el-tag v-if="seeOperationInfoForm.operationStatus === '1'" type="danger">失败</el-tag>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="createTime" label="操作时间:">
                <span>{{seeOperationInfoForm.createTime}}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="seeOperationInfoVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import * as sysOperationInfoServer from '@/api/sysmonitor'

export default {
  components: { Pagination },
  data() {
    return {
      searchForm: {
        username: '',
        empName: '',
        systemModules: '',
        operationType: '',
        operationStatus: '',
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      sysOperationInfoData: [],
      address: '',
      seeOperationInfoVisible: false,
      seeOperationInfoForm: {
        deptName: '',
        systemModules: '',
        operationType: '',
        requestMethod: '',
        requestAddress: '',
        username: '',
        empName: '',
        ipAddress: '',
        operationLocation: '',
        operationStatus: '',
        createTime: ''
      },
    }
  },

  created() {
    this.handleSearch()
  },

  methods: {
    handleSearch() {
      sysOperationInfoServer.findSysOperationInfo(this.searchForm).then(res => {
        if (res.code === 200 && res.result) {
          this.sysOperationInfoData = res.result.records || []
          this.sysOperationInfoData.map((item) => {
            if (item.operationLocation === 'XX XX'){
              // fetch(`https://api.vvhan.com/api/getIpInfo?ip=${item.ipAddress}`).then((res) => res.json().then((data) => {
              //     if (data.success === true){
              //       if (data.info.country === '局域网' || data.info.country === '本机地址'){
              //         this.address = '内网IP'
              //       }else {
              //         this.address = data.info.prov + '省 ' + data.info.city
              //       }
              //       this.$set(item, 'operationLocation', this.address)
              //     }
              //   })
              // )


              //通过ip地址获取所在地
              fetch(`https://api.vore.top/api/IPdata?ip=${item.ipAddress}`).then((res) => res.json().then((data) => {
                  if (data.msg === 'SUCCESS'){
                    if (data.ipdata.info1 === '保留IP'){
                      this.address = '内网IP'
                    }else {
                      this.address = data.ipdata.info1 + ' ' + data.ipdata.info2
                    }
                    this.$set(item, 'operationLocation', this.address)
                  }
                })
              )


            }else {
              this.$set(item, 'operationLocation', item.operationLocation)
            }
          })
          this.total = res.result.total
        }
      })
    },

    // 详细
    seeOperationInfo(row) {
      this.seeOperationInfoForm.deptName = row.deptName
      this.seeOperationInfoForm.systemModules = row.systemModules
      this.seeOperationInfoForm.operationType = row.operationType
      this.seeOperationInfoForm.requestMethod = row.requestMethod
      this.seeOperationInfoForm.requestAddress = row.requestAddress
      this.seeOperationInfoForm.username = row.username
      this.seeOperationInfoForm.empName = row.empName
      this.seeOperationInfoForm.ipAddress = row.ipAddress
      this.seeOperationInfoForm.createTime = row.createTime
      this.seeOperationInfoForm.operationLocation = row.operationLocation
      this.seeOperationInfoForm.operationStatus = row.operationStatus
      this.seeOperationInfoVisible = true
    },

  }
}
</script>

<style scoped lang="scss" type="text/scss">
.search_div {
  >div {
    &:nth-child(1){
      margin-right: 10px;
    }
    display: inline-block;
  }
}
.search_input {
  width: 200px;
  margin-bottom: 5px;
}
.search-right {
  text-align: right;
  padding-right: 25px;
}
.item {
  font-size: 16px;
}
::v-deep .el-card .el-card__header {
  padding: 5px 20px;
  background: #F3F2F2;
}
.apostrophe{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.table2  {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
/deep/ .vue-treeselect--disabled .vue-treeselect__single-value{
  color: #909399
}
</style>
